package control
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	
	import org.osmf.events.TimeEvent;
	
	import view.Arrow;
	
	public class Arrowing extends Sprite
	{
		public function Arrowing(_arrow:Arrow, _bd:Point, _monster_arr:Array)
		{
			arrow = _arrow;
			monsterArr = _monster_arr;
			point = _bd
			arrowSpeed = arrow.speed;
			shottingRadius = arrow.shottingRadius;
			var timer:Timer = new Timer(arrowSpeed);
			timer.start();
			timer.addEventListener(TimerEvent.TIMER,rangeListener);
			
		}
		/**
		 *箭 
		 */		
		private var arrow:Arrow;
		/**
		 *所有怪物的数组 
		 */		
		public var monsterArr:Array;
		/**
		 *发射速度 
		 */		
		private var arrowSpeed:Number;
		/**
		 *当前点 
		 */		
		private var point:Point;
		/**
		 *攻击范围 
		 */		
		private var shottingRadius:uint
		
		private var monster:MonsterControl;
		private var dy:Number;
		private var dx:Number;
		private var dist:uint;
		private var index:uint = 0;
		
		/**
		 *检测是否进入范围 
		 * @param event
		 * 
		 */		
		protected function rangeListener(event:TimerEvent):void
		{
			for (var i:uint = 0; i < monsterArr.length; i++) 
			{
				monster = monsterArr[i]
				dy = point.y - monsterArr[i].y;
				dx = point.x - monsterArr[i].x;
				dist = Math.sqrt(dx*dx+dy*dy);
				if(dist<arrow.shottingRadius) 
				{
					arrow.monster = monster
					arrow.start = point;
					arrow.end = monster.crtPoint;
					
					return;
				}
			}
		}
		
	}
}